#include "Device.h"
#include "DeviceManager.h"
#include <iostream>

using namespace std;

// 构造函数
Device::Device(int id, DeviceType type)
    : id(id), type(type), status("off") {}

// 获取设备ID
int Device::getId() const {
    return id;
}

// 获取设备类型
Device::DeviceType Device::getType() const {
    return type;
}

// 获取设备状态
const string& Device::getStatus() const {
    return status;
}

// 设置设备状态
void Device::setStatus(const string& newStatus) {
    status = newStatus;
}

// 灯具类实现
Light::Light(int id) : Device(id, Device::DeviceType::LIGHT), brightness(0) {}

void Light::turnOn() {
    setStatus("on");
    std::cout << "Light " << getId() << " turned on." << std::endl;
}

void Light::turnOff() {
    setStatus("off");
    std::cout << "Light " << getId() << " turned off." << std::endl;
}

void Light::setBrightness(int brightness) {
    if (brightness >= 0 && brightness <= 100) {
        this->brightness = brightness;
        std::cout << "Light " << getId() << " brightness set to " << brightness << "%" << std::endl;
    } else {
        std::cout << "Invalid brightness value." << std::endl;
    }
}

// 温控器类实现
Thermostat::Thermostat(int id) : Device(id, Device::DeviceType::THERMOSTAT), currentTemperature(20.0), targetTemperature(20.0) {}

void Thermostat::turnOn() {
    setStatus("on");
    std::cout << "Thermostat " << getId() << " turned on." << std::endl;
}

void Thermostat::turnOff() {
    setStatus("off");
    std::cout << "Thermostat " << getId() << " turned off." << std::endl;
}

void Thermostat::setTemperature(double temperature) {
    targetTemperature = temperature;
    std::cout << "Thermostat " << getId() << " temperature set to " << temperature << "°C" << std::endl;
}

double Thermostat::getCurrentTemperature() const {
    return currentTemperature;
}

// 摄像头类实现
Camera::Camera(int id) : Device(id, Device::DeviceType::CAMERA), resolution("720p") {}

void Camera::turnOn() {
    setStatus("on");
    std::cout << "Camera " << getId() << " turned on." << std::endl;
}

void Camera::turnOff() {
    setStatus("off");
    std::cout << "Camera " << getId() << " turned off." << std::endl;
}

void Camera::setResolution(const std::string& resolution) {
    this->resolution = resolution;
    std::cout << "Camera " << getId() << " resolution set to " << resolution << std::endl;
}

// 插座类实现
Socket::Socket(int id) : Device(id, Device::DeviceType::SOCKET) {}

void Socket::turnOn() {
    setStatus("on");
    std::cout << "Socket " << getId() << " turned on." << std::endl;
}

void Socket::turnOff() {
    setStatus("off");
    std::cout << "Socket " << getId() << " turned off." << std::endl;
}

// 设备工厂类实现
Device* DeviceFactory::createDevice(Device::DeviceType type, int id) {
    switch (type) {
        case Device::DeviceType::LIGHT:
            return new Light(id);
        case Device::DeviceType::THERMOSTAT:
            return new Thermostat(id);
        case Device::DeviceType::CAMERA:
            return new Camera(id);
        case Device::DeviceType::SOCKET:
            return new Socket(id);
        default:
            return nullptr;
    }
}
